#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "main_interface.h"
#include "setting.h"
#include "functions.h"
#include "Util.h"
#include "setting.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 1. 加载QStacked的相关内容
    // 主界面
    Main_Interface* main_interface = new Main_Interface();
    ui->mainStackedWidget->insertWidget(0, main_interface);
    // 设置
    Setting* setting = new Setting();
    ui->mainStackedWidget->insertWidget(1, setting);
    // 功能列表
    Functions* func = new Functions();
    ui->mainStackedWidget->insertWidget(2, func);

    // 设置当前显示的页面
    ui->mainStackedWidget->setCurrentIndex(0); // 确保显示正确的初始页面
    // 2. 连接信号槽函数
    connect(setting, &Setting::colorChanged, this, &MainWindow::changeBackgroundColor);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::switchPage()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender()); // 将发出信号的对象进行类型转换
    if(button == ui->mainInterface_btn) {
        ui->mainStackedWidget->setCurrentIndex(0);
    }else if(button == ui->setting_btn) {
        ui->mainStackedWidget->setCurrentIndex(1);
    }else if(button == ui->functions_btn){
        ui->mainStackedWidget->setCurrentIndex(2);
    }else {
        QMessageBox::warning(this, "Error", "未知的页码");
    }
}

void MainWindow::on_mainInterface_btn_clicked()
{
    qDebug() << "点击了主页";
    switchPage();
}

void MainWindow::on_setting_btn_clicked()
{
    qDebug() << "点击了设置";
    switchPage();
}

void MainWindow::on_functions_btn_clicked()
{
    qDebug() << "点击了功能汇总";
    switchPage();
}

void MainWindow::changeBackgroundColor(const QColor &color)
{
    setStyleSheet("QMainWindow { background-color: " + color.name() + "; }");
}
